package com.lianxueda.drawing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lianxueda.drawing.common.R;
import com.lianxueda.drawing.domain.dto.ClassQueryDTO;
import com.lianxueda.drawing.domain.dto.NotificationQueryDTO;
import com.lianxueda.drawing.domain.pojo.Classes;
import com.lianxueda.drawing.domain.pojo.Notifications;
import com.lianxueda.drawing.domain.vo.PageVO;
import com.lianxueda.drawing.mapper.NotificationMapper;
import com.lianxueda.drawing.service.NotificationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/notice")
@Tag(name = "通知管理模块", description = "通知管理模块接口")
@RequiredArgsConstructor
public class NotificationController {

    @Resource
    private NotificationService notificationService;
    @Resource
    private NotificationMapper notificationMapper;

    //新增通知
    @Operation(summary = "新增通知")
    @PostMapping("/add")
    public R addNotice(@RequestBody Notifications notifications) {
        if (notificationMapper.selectCount(new QueryWrapper<Notifications>().eq("title", notifications.getTitle())) > 0) {
            return R.fail("该通知已存在！");
        }
        notificationService.save(notifications);
        return R.suc("通知新增成功！");
    }

    //删除通知
    @Operation(summary = "删除通知")
    @DeleteMapping("/delete")
    public R deleteNotice(@RequestParam Integer id) {
        if (notificationService.getById(id) == null) {
            return R.fail("该通知不存在！");
        }
        notificationService.removeById(id);
        return R.suc("删除成功！");
    }

    //修改通知
    @Operation(summary = "修改通知")
    @PutMapping("/update")
    public R updateNotice(@RequestBody Notifications notifications) {
        if (notificationService.getById(notifications.getId()) == null) {
            return R.fail("该通知不存在！");
        }
        notificationService.updateById(notifications);
        return R.suc("修改成功！");
    }

    //分页查询通知
    @Operation(summary="分页查询通知")
    @PostMapping("/listPage")
    public R<List<Notifications>> listPage(@RequestBody NotificationQueryDTO notificationQueryDTO) {
        return R.suc(notificationService.findByPage(notificationQueryDTO));
    }
}
